// 时间20161108160023 转为 2016-11-08 16:00:23
function getDateStrFormate1(dateString, type) {
    dateString = String(dateString);
    var pattern = /(\d{4})(\d{2})(\d{2})/;
    var formatedDate;
    if (type == "time") {
        pattern = /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})/;
        formatedDate = dateString.substr(0, 12).replace(pattern, '$1-$2-$3 $4:$5');
    } else if (type == "second") {
        pattern = /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/;
        formatedDate = dateString.substr(0, 16).replace(pattern, '$1-$2-$3 $4:$5:$6');
    } else if (type == "date") {
        formatedDate = dateString.substr(0, 8).replace(pattern, '$1-$2-$3');
    }

    return formatedDate;
}


/*

format :"yyyy-mm-dd hh:ii:ss";

  return 2015-11-05 17:39:23
format :"yyyy-mm-dd";

    return 2015-11-05
format :"yyyy/mm/dd hh/ii/ss";

    return 2015/11/05 17/39/23
format :"yyyy/mm/dd";

  return 2015/11/05

 */
function getDateStrFormate2(formart, newDate) {
    var dateSty = arguments[0].split(' '),
        separatorFirst = '',
        separatorSecond = '',
        realDate = '';
    var yyyy = newDate.getFullYear(),
        mm = newDate.getMonth() + 1,
        dd = newDate.getDate(),
        hh = newDate.getHours(),
        ii = newDate.getMinutes(),
        ss = newDate.getSeconds();
    mm = mm < 10 ? '0' + mm : mm;
    dd = dd < 10 ? '0' + dd : dd;
    hh = hh < 10 ? '0' + hh : hh;
    ii = ii < 10 ? '0' + ii : ii;
    ss = ss < 10 ? '0' + ss : ss;

    if (dateSty.length > 1) {
        console.log(dateSty);
        separatorFirst = dateSty[0].charAt(4);
        separatorSecond = dateSty[1].charAt(2);
        realDate = yyyy + separatorFirst + mm + separatorFirst + dd + ' ' + hh + separatorSecond + ii + separatorSecond + ss;
    } else {
        separatorFirst = dateSty[0].charAt(4);
        realDate = yyyy + separatorFirst + mm + separatorFirst + dd;
    }

    return realDate;
}

// 年份大于现在 参数字符串数字'16':2016 ,16:2016。年份相同也是大于
function isNextYear(yearStr) {
    var isNext = "more"; // 默认年大于现在
    var theNow = new Date();
    yearStr = yearStr.toString();

    if (yearStr.length == 4) {
        if (theNow.getFullYear() > parseInt(yearStr)) {
            isNext = "less"; // 年份小于现在
        } else if (theNow.getFullYear() == parseInt(yearStr)) {
            isNext == "same" // 同样年份
        }
    } else if (yearStr.length > 4 || yearStr.length == 0) {
        isNext = "erro"; // 位数错误
    } else {
        var theYear2 = theNow.getFullYear().toString().substr(2, 4);
        if (parseInt(theYear2) > parseInt(yearStr)) {
            isNext = "less";
        } else if (parseInt(theYear2) == parseInt(yearStr)) {
            isNext = "same" // 同样年份
        }
    }

    return isNext;
}

// 月份是否大于 现在
function isNextMonth(monthStr) {
    var isNext = true;
    var theNow = new Date();
    if (theNow.getMonth() > parseInt(monthStr) - 1) {
        isNext = false;
    }
    return isNext;
}

function isNextYearMonth(yearStr, monthStr) {
    var isMoreYear = isNextYear(yearStr);
    if (isMoreYear == "more") {
        return true;
    } else if (isMoreYear == "less") {
        return false;
    } else if (isMoreYear == "same") {
        if (isNextMonth(monthStr)) {
            return true;
        }
    } else {
        return false;
    }
}



module.exports = {
    getDateStrFormate1: getDateStrFormate1,
    getDateStrFormate2: getDateStrFormate2,
    isNextYear: isNextYear,
    isNextMonth: isNextMonth,
    isNextYearMonth: isNextYearMonth
};